package org.zp.handler;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.Paging;
import org.zp.pojo.AdminInfo;
import org.zp.pojo.UserInfo;
import org.zp.service.AdminLogInfoService;
import org.zp.service.UserInfoService;
import org.zp.service.UserInfoServiceImpl;
import org.zp.util.Ajax;
import org.zp.util.Security;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
public class UserHandler {
@Resource
private UserInfoService u;
@Resource
private AdminLogInfoService log;
private String url = "用户列表";
/**
 * 获得用户列表
 * @param req
 * @param resp
 */
@GetMapping("selectGK.act")
public void selectUser(HttpServletRequest req, HttpServletResponse resp) {
	List<UserInfo> list = u.showGKuser();
	Ajax.success(resp, "list", list);
}
/**
 * 用户多条件查询
 * @param name
 * @param sex
 * @param start
 * @param end
 * @param req
 * @param resp
 */
@GetMapping("getUserlist.act")
public void getUserlist(Paging dto,String name,String sex,String start,String end,
		HttpServletRequest req, HttpServletResponse resp) {
	Integer startpage = Integer.parseInt(dto.getStartpage());
	Integer limitpage = Integer.parseInt(dto.getLimitpage());
	Long iSex = (long) Integer.parseInt(sex);
	if (name.equals("")) {
		name = null;
	}
	if (iSex==-1||iSex==2) {
		iSex = null;
	}
	if (start.equals("")) {
		start = null;
	}
	if (end.equals("")) {
		end = null;
	}
	if (start!=null&&end!=null) {
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");//日期格式处理
    try {
			Date star = df.parse(start);
			Date endd = df.parse(end);
			if (star.getTime()>endd.getTime()){//比较时间的大小
				Ajax.error(resp,"时间有误",10001);
				return;
        }
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	List<UserInfo> list = u.selectuser(startpage, limitpage,name,iSex,start,end);
	PageInfo<UserInfo>pageInfo = new PageInfo<UserInfo>(list);
	Integer count = (int) pageInfo.getTotal();
	System.out.println("用户多条件查询count:"+count);
	Ajax.success(resp, "list","count", list,count);
}
/**
 * 用户修改状态
 * @param status
 * @param uid
 * @param req
 * @param resp
 */
@PostMapping("userEdit.act")
public void userEdit(String status,String uid,
		HttpServletRequest req, HttpServletResponse resp) {
	int Uid = 0;
	
	try {
		Uid = Integer.parseInt(uid);
	} catch (NumberFormatException e) {
		Ajax.error(resp, "id错误");
		e.printStackTrace();
		return;
	}
	
	//初始化选择器的值
	int iStatus = 0;
	
	try {
		iStatus = Integer.parseInt(status);
	} catch (NumberFormatException e) {
		Ajax.error(resp, "状态错误",10000);
		e.printStackTrace();
		return;
	}


	Integer index = u.updateUserState(Uid,iStatus);
	System.out.println("修改结果:"+index);
	if (index>0) {
		Ajax.success(resp);
		System.out.println("修改成功");
		AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid = admin.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		return;
	}else {
		Ajax.error(resp, "修改失败",10001);
		System.out.println("修改失败");
	}
	
}
/**
 * 删除用户
 * @param uid
 * @param req
 * @param resp
 */
@PostMapping("userDel.act")
public void userDel(String uid,
		HttpServletRequest req, HttpServletResponse resp) {
	int Uid = 0;
	try {
		Uid = Integer.parseInt(uid);
	} catch (NumberFormatException e) {
		Ajax.error(resp, "用户id不正确", 10000);
	}
	if(Uid <=0) {
		Ajax.error(resp, "用户id不正确", 10000);
		return;
	}

	Integer index = u.deleteUser(Uid);
	if (index>0) {
		Ajax.success(resp);
		System.out.println("删除成功");
		//删除成功后加入日志表记录表中
		AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid = admin.getAdminId();
		Integer cid = 2;//此为删除的操作id
		Integer logs = log.addlog(aid,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		return;
	}else {
		Ajax.error(resp, "删除失败",10001);
		System.out.println("删除失败");
	}
}
/**
 * 重置用户密码
 * @param uid
 * @param req
 * @param resp
 */
@PostMapping("userNewPwd.act")
public void userNewPwd(String uid,HttpServletRequest req, HttpServletResponse resp) {
	int Uid = 0;
	try {
		Uid = Integer.parseInt(uid);
	} catch (NumberFormatException e) {
		Ajax.error(resp, "用户id不正确", 10000);
	}
	if(Uid <=0) {
		Ajax.error(resp, "用户id不正确", 10000);
		return;
	}
	String pwd = "a123456";
	Security md5 = new Security();
	String md5pwd = md5.encrypt16(pwd);
	Integer index = u.newUserpwd(Uid,md5pwd);
	if (index>0) {
		Ajax.success(resp);
		System.out.println("重置成功");
		AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid = admin.getAdminId();
		Integer cid = 3;//此为修改的操作id
		Integer logs = log.addlog(aid,cid,url);
		if (logs>0) {
			System.out.println("加入日志成功");
		}else {
			System.out.println("加入日志失败");
			return;
		}
		return;
	}else {
		Ajax.error(resp, "重置失败",10001);
		System.out.println("重置失败");
	}
}

}
